package com.one.rope.mvp.web.business.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.one.rope.mvp.basecore.util.MyBeanUtils;
import com.one.rope.mvp.basecore.util.StringUtils;
import com.one.rope.mvp.web.business.entity.MemberInfo;
import com.one.rope.mvp.web.business.dao.MemberInfoMapper;
import com.one.rope.mvp.web.business.service.IMemberInfoService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.one.rope.mvp.web.common.dto.admin.member.*;
import com.one.rope.mvp.basecore.bean.AdminBusinessMessage;
import com.one.rope.mvp.basecore.bean.ResponseObject;
import com.one.rope.mvp.basecore.exception.BusinessDataException;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.List;

/**
 * <p>
 * 服务实现类
 * </p>
 *
 * @author wesley
 * @since 2020-02-20
 */
@Service
public class MemberInfoServiceImpl extends ServiceImpl<MemberInfoMapper, MemberInfo> implements
    IMemberInfoService {

  @Autowired
  MemberInfoMapper memberInfoMapper;

  @Override
  public ResponseObject memberInfoList(AdminMemberInfoListDTO requestDTO) {
    AdminMemberInfoListVO resultVO = new AdminMemberInfoListVO();
    Page<AdminMemberInfoListItemVO> page = new Page<>(requestDTO.getPageNo(),
        requestDTO.getPageSize());
    List<AdminMemberInfoListItemVO> recordList = memberInfoMapper
        .selectAdminMemberInfoListByPage(page, requestDTO);
    resultVO.setResults(recordList);
    resultVO.initPageInfo(page);
    return new ResponseObject(resultVO);
  }

  @Override
  public ResponseObject personInfoList(AdminPersonInfoListDTO requestDTO) {
    AdminPersonInfoListVO resultVO = new AdminPersonInfoListVO();
    Page<AdminPersonInfoListItemVO> page = new Page<>(requestDTO.getPageNo(),
        requestDTO.getPageSize());
    List<AdminPersonInfoListItemVO> recordList = memberInfoMapper
        .selectAdminPersonInfoListByPage(page, requestDTO);
    resultVO.setResults(recordList);
    resultVO.initPageInfo(page);
    return new ResponseObject(resultVO);
  }

  @Override
  public ResponseObject editPersonInfo(AdminEditPersonInfoDTO requestDTO) {
    // 校验手机号是否已经存在
    MemberInfo dbMemberInfo = this.getOne(new QueryWrapper<MemberInfo>().
        eq("mobile", requestDTO.getMobile()).
        ne("id", requestDTO.getId()));
    if (null != dbMemberInfo) {
      throw new BusinessDataException(AdminBusinessMessage.MOBILE_HAVE_EXIST);
    }
    MemberInfo memberInfo = new MemberInfo();
    MyBeanUtils.copyPropertiesIgnoreNull(requestDTO, memberInfo);
    this.updateById(memberInfo);
    return ResponseObject.successObject();
  }

  @Override
  public ResponseObject memberList(AdminMemberListDTO requestDTO) {
    List<AdminMemberListVO> resultVO = new ArrayList<>();
    QueryWrapper<MemberInfo> queryWrapper = new QueryWrapper<>();
    if (!StringUtils.isNullOrEmpty(requestDTO.getName())) {
      queryWrapper.like("name", requestDTO.getName());
    }
    if (null != requestDTO.getType() && requestDTO.getType() > 0) {
      queryWrapper.like("user_type", requestDTO.getType());
    }
    queryWrapper.eq("is_delete", 0);
    List<MemberInfo> memberInfoList = this.list(queryWrapper);
    memberInfoList.stream().forEach(item -> {
      AdminMemberListVO memberListVO = new AdminMemberListVO();
      MyBeanUtils.copyPropertiesIgnoreNull(item, memberListVO);
      resultVO.add(memberListVO);
    });
    return new ResponseObject(resultVO);
  }

  @Override
  public ResponseObject editMemberInfo(AdminEditMemberInfoDTO requestDTO) {
    MemberInfo memberInfo = new MemberInfo();
    MyBeanUtils.copyPropertiesIgnoreNull(requestDTO, memberInfo);
    this.updateById(memberInfo);
    return ResponseObject.successObject();
  }

  @Override
  public ResponseObject deletePersonInfo(Long id) {
    MemberInfo dbMemberInfo = this.getById(id);
    if (null == dbMemberInfo) {
      throw new BusinessDataException(AdminBusinessMessage.MEMBER_NOT_EXIST);
    }
    MemberInfo memberInfo = new MemberInfo();
    memberInfo.setId(id);
    memberInfo.setIsDelete(1);
    this.updateById(memberInfo);
    return ResponseObject.successObject();
  }
}
